-- =============================================
-- Create a function to insert new CHIETKHAU
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_CHIETKHAU' 
)
   DROP PROCEDURE dbo.NEW_CHIETKHAU
GO

CREATE PROCEDURE dbo.NEW_CHIETKHAU
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'CK'

	SELECT TOP 1 @LastID = MaChietKhau 
		FROM dbo.CHIETKHAU
		ORDER BY RIGHT(MaChietKhau, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.CHIETKHAU
		SET MaChietKhau = @NextID
		WHERE MaChietKhau = ''
GO

-- =============================================
-- Create a function to insert new CTHOADON
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_CTHOADON' 
)
   DROP PROCEDURE dbo.NEW_CTHOADON
GO

CREATE PROCEDURE dbo.NEW_CTHOADON
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'CH'

	SELECT TOP 1 @LastID = MaChiTiet 
		FROM dbo.CTHOADON
		ORDER BY RIGHT(MaChiTiet, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.CTHOADON
		SET MaChiTiet = @NextID
		WHERE MaChiTiet = ''
GO

-- =============================================
-- Create a function to insert new CTPHIEUNHAP
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_CTPHIEUNHAP' 
)
   DROP PROCEDURE dbo.NEW_CTPHIEUNHAP
GO

CREATE PROCEDURE dbo.NEW_CTPHIEUNHAP
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'CP'

	SELECT TOP 1 @LastID = MaChiTiet 
		FROM dbo.CTPHIEUNHAP
		ORDER BY RIGHT(MaChiTiet, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.CTPHIEUNHAP
		SET MaChiTiet = @NextID
		WHERE MaChiTiet = ''
GO

-- =============================================
-- Create a function to insert new GIANIEMYET
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_GIANIEMYET' 
)
   DROP PROCEDURE dbo.NEW_GIANIEMYET
GO

CREATE PROCEDURE dbo.NEW_GIANIEMYET
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'GB'

	SELECT TOP 1 @LastID = MaGia
		FROM dbo.GIANIEMYET
		ORDER BY RIGHT(MaGia, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.GIANIEMYET
		SET MaGia = @NextID
		WHERE MaGia = ''
GO

-- =============================================
-- Create a function to insert new HOADON
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_HOADON' 
)
   DROP PROCEDURE dbo.NEW_HOADON
GO

CREATE PROCEDURE dbo.NEW_HOADON
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'HD'

	SELECT TOP 1 @LastID = MaHoaDon 
		FROM dbo.HOADON
		ORDER BY RIGHT(MaHoaDon, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.HOADON
		SET MaHoaDon = @NextID
		WHERE MaHoaDon = ''
GO

-- =============================================
-- Create a function to insert new LOAIMATHANG
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_LOAIMATHANG' 
)
   DROP PROCEDURE dbo.NEW_LOAIMATHANG
GO

CREATE PROCEDURE dbo.NEW_LOAIMATHANG
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'LM'

	SELECT TOP 1 @LastID = MaLoai 
		FROM dbo.LOAIMATHANG
		ORDER BY RIGHT(MaLoai, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.LOAIMATHANG
		SET MaLoai = @NextID
		WHERE MaLoai = ''
GO

-- =============================================
-- Create a function to insert new LOAINHANVIEN
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_LOAINHANVIEN' 
)
   DROP PROCEDURE dbo.NEW_LOAINHANVIEN
GO

CREATE PROCEDURE dbo.NEW_LOAINHANVIEN
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'LN'

	SELECT TOP 1 @LastID = MaLoai 
		FROM dbo.LOAINHANVIEN
		ORDER BY RIGHT(MaLoai, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.LOAINHANVIEN
		SET MaLoai = @NextID
		WHERE MaLoai = ''
GO

-- =============================================
-- Create a function to insert new LUONGNV
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_LUONGNV' 
)
   DROP PROCEDURE dbo.NEW_LUONGNV
GO

CREATE PROCEDURE dbo.NEW_LUONGNV
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'LV'

	SELECT TOP 1 @LastID = MaLuong 
		FROM dbo.LUONGNV
		ORDER BY RIGHT(MaLuong, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.LUONGNV
		SET MaLuong = @NextID
		WHERE MaLuong = ''
GO

-- =============================================
-- Create a function to insert new MATHANG
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_MATHANG' 
)
   DROP PROCEDURE dbo.NEW_MATHANG
GO

CREATE PROCEDURE dbo.NEW_MATHANG
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'MH'

	SELECT TOP 1 @LastID = MaMatHang 
		FROM dbo.MATHANG
		ORDER BY RIGHT(MaMatHang, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.MATHANG
		SET MaMatHang = @NextID
		WHERE MaMatHang = ''
GO

-- =============================================
-- Create a function to insert new NHANVIEN
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_NHANVIEN' 
)
   DROP PROCEDURE dbo.NEW_NHANVIEN
GO

CREATE PROCEDURE dbo.NEW_NHANVIEN
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'NV'

	SELECT TOP 1 @LastID = MaNhanVien 
		FROM dbo.NHANVIEN
		ORDER BY RIGHT(MaNhanVien, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.NHANVIEN
		SET MaNhanVien = @NextID
		WHERE MaNhanVien = ''
GO

-- =============================================
-- Create a function to insert new PHANQUYEN
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_PHANQUYEN' 
)
   DROP PROCEDURE dbo.NEW_PHANQUYET
GO

CREATE PROCEDURE dbo.NEW_PHANQUYEN
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'PQ'

	SELECT TOP 1 @LastID =  MaPhanQuyen
		FROM dbo.PHANQUYEN
		ORDER BY RIGHT(MaPhanQuyen, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.PHANQUYEN
		SET MaPhanQuyen = @NextID
		WHERE MaPhanQuyen = ''
GO

-- =============================================
-- Create a function to insert new PHATNV
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_PHATNV' 
)
   DROP PROCEDURE dbo.NEW_PHATNV
GO

CREATE PROCEDURE dbo.NEW_PHATNV
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'PV'

	SELECT TOP 1 @LastID = MaPhat
		FROM dbo.PHATNV
		ORDER BY RIGHT(MaPhat, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.PHATNV
		SET MaPhat = @NextID
		WHERE MaPhat = ''
GO

-- =============================================
-- Create a function to insert new PHIEUNHAP
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_PHIEUNHAP' 
)
   DROP PROCEDURE dbo.NEW_PHIEUNHAP
GO

CREATE PROCEDURE dbo.NEW_PHIEUNHAP
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'PN'

	SELECT TOP 1 @LastID = MaPhieu 
		FROM dbo.PHIEUNHAP
		ORDER BY RIGHT(MaPhieu, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.PHIEUNHAP
		SET MaPhieu = @NextID
		WHERE MaPhieu = ''
GO

-- =============================================
-- Create a function to insert new TAMUNG
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_TAMUNG' 
)
   DROP PROCEDURE dbo.NEW_TAMUNG
GO

CREATE PROCEDURE dbo.NEW_TAMUNG
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'TU'

	SELECT TOP 1 @LastID = MaTamUng 
		FROM dbo.TAMUNG
		ORDER BY RIGHT(MaTamUng, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.TAMUNG
		SET MaTamUng = @NextID
		WHERE MaTamUng = ''
GO

-- =============================================
-- Create a function to insert new THUONGNV
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_THUONGNV' 
)
   DROP PROCEDURE dbo.NEW_THUONGNV
GO

CREATE PROCEDURE dbo.NEW_THUONGNV
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'TV'

	SELECT TOP 1 @LastID = MaThuong 
		FROM dbo.THUONGNV
		ORDER BY RIGHT(MaThuong, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.THUONGNV
		SET MaThuong = @NextID
		WHERE MaThuong = ''
GO

-- =============================================
-- Create a function to insert new TKDANGNHAP
-- =============================================
USE DAILY
GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_SCHEMA = N'dbo'
     AND SPECIFIC_NAME = N'NEW_TKDANGNHAP' 
)
   DROP PROCEDURE dbo.NEW_TKDANGNHAP
GO

CREATE PROCEDURE dbo.NEW_TKDANGNHAP
	@NextID char(10) OUTPUT
AS
	DECLARE @LastID char(10)
	DECLARE @Prefix char(2) = 'TD'

	SELECT TOP 1 @LastID = MaTaiKhoan 
		FROM dbo.TKDANGNHAP
		ORDER BY RIGHT(MaTaiKhoan, 8) DESC

	SET @NextID = dbo.FN_NextID (@LastID, @Prefix, 10)

    UPDATE dbo.TKDANGNHAP
		SET MaTaiKhoan = @NextID
		WHERE MaTaiKhoan = ''
GO